#include "managefavoritesdialog.h"
#include "ui_managefavoritesdialog.h"
#include <QFileDialog>
ManageFavoritesDialog::ManageFavoritesDialog(QWidget *parent,QStringList *favorites)
    : QDialog(parent)
    , ui(new Ui::ManageFavoritesDialog)
    ,favorites(favorites)
{
    ui->setupUi(this);

    // 初始化列表控件
    for (const QString &path : *favorites) {
        ui->listWidget->addItem(path);
    }


}

ManageFavoritesDialog::~ManageFavoritesDialog()
{
    delete ui;
}

void ManageFavoritesDialog::on_btnAdd_clicked()
{
    QString path = QFileDialog::getOpenFileName(this, tr("选择文件"), "", tr("所有文件 (*)"));
    if (!path.isEmpty()) {
        ui->listWidget->addItem(path);
        favorites->append(path);
    }
}


void ManageFavoritesDialog::on_btnDelete_clicked()
{
    QListWidgetItem *item = ui->listWidget->currentItem();
    if (item) {
        int row = ui->listWidget->row(item);
        ui->listWidget->takeItem(row);
        favorites->removeAt(row);
    }
}


void ManageFavoritesDialog::on_btnEdit_clicked()
{
    QListWidgetItem *item = ui->listWidget->currentItem();
    if (item) {
        QString path = QFileDialog::getOpenFileName(this, tr("选择文件"), item->text(), tr("所有文件 (*)"));
        if (!path.isEmpty()) {
            item->setText(path);
            int row = ui->listWidget->row(item);
            favorites->replace(row, path);
        }
    }
}


void ManageFavoritesDialog::on_btnOK_clicked()
{
    accept();
}

